import dt from 'moment';
import lodash from 'lodash';
import log from '../../../log';
import * as data from './data';
import * as util from '../../../util';

const likeParms = 'name'.split(',');
function create(user, info) {
    //问卷模板
    const queTemp = lodash.cloneDeep(info);
    queTemp.id = util.uuid();
    queTemp.createTime = new Date();
    queTemp.createUserId = user.id;
    queTemp.createUserName = user.name;
    queTemp.status = info.status ? info.status : 1;
    delete queTemp.questions;

    log.info(`${user.name}创建问卷${info.title}`);

    //问题
    const questions = [];
    const options = [];
    info.questions.forEach(q => {
        const que = lodash.cloneDeep(q);
        que.id = util.uuid();
        que.queTempId = queTemp.id;
        delete que.options;
        questions.push(que);
        //选项
        q.options.forEach(o => {
            const opt = lodash.cloneDeep(o);
            opt.id = util.uuid();
            opt.questionId = que.id;
            options.push(opt);
        });
    });
    return data.create(queTemp, questions, options).then(() => queTemp.id);
}
function edit(id, info) {
    //问卷模板
    const queTemp = lodash.cloneDeep(info);
    delete queTemp.questions;
    log.info(`修改问卷, queTemplateId=${id}, ${info.title}`);
    //问题
    const questionsOld = [];
    const questionsNew = [];
    const options = [];
    info.questions.forEach(q => {
        const que = lodash.cloneDeep(q);
        //判断是否有id，有id的是修改，没id的是创建
        if (que.id) {
            que.queTempId = queTemp.id;
            delete que.options;
            questionsOld.push(que);
        } else {
            que.id = util.uuid();
            que.queTempId = queTemp.id;
            delete que.options;
            questionsNew.push(que);
        }
        //选项
        q.options.forEach(o => {
            const opt = lodash.cloneDeep(o);
            opt.id = util.uuid();
            opt.questionId = que.id;
            options.push(opt);
        });
    });
    return data.edit(id, queTemp, questionsOld, questionsNew, options).then(() => queTemp.id);
}
function updateStatus(id, status) {
    return data.updateStatus(id, status);
}
function del(user, id) {
    log.info(`${user.name}删除问卷${id}`);
    return data.del(id).then(util.noop);
}
// 获取问卷模板列表
function get(page, pageSize, query, user) {
    return data.get(user, query, page, pageSize).then(data => {
        return util.data(data, 1);
    });
}

function find(query) {
    return util.resolve().then(() => {
        log.info('find quesTemp params----', query);
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.find(where, 1).then(res => {
            log.info('find quesTemp result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('find quesTemp is err----', err);
        return err;
    });
}
function getCountByUserId(user) {
    return data.getCountByUserId(user.id).then(queCount => {
        return queCount;
    });
}

function push(id) {
    return util.resolve().then(() => {
        log.info('push quesTemp  params----', id);
        const queTemp = {id, status: 2, isPub: 1, publishTime: dt().format('YYYY-MM-DD HH:mm:ss')};
        return data.edit(id, queTemp).then(res => {
            log.info('push quesTemp  result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('push quesTemp is err----', err);
        return err;
    });
}
function end(id) {
    return util.resolve().then(() => {
        log.info('end quesTemp  params----', id);
        const questionnaires = {status: 3, id};
        return data.edit(questionnaires.id, questionnaires).then(res => {
            log.info('end quesTemp  result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('end quesTemp is err----', err);
        return err;
    });
}

function getQueDetailsAndAnswers(queTempId, user) {
    return util.resolve().then(() => {
        return data.getQueDetailsAndAnswers(queTempId, user);
    });
}

export {create, edit, updateStatus, del, get, find, push, end, getCountByUserId, getQueDetailsAndAnswers};
